package com.example.gray.controller;

import com.example.gray.entity.IdList;
import com.example.gray.entity.LogList;
import com.example.gray.entity.OpLog;
import com.example.gray.entity.Result;
import com.example.gray.service.OpLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author Gray 2021/12/28-18:28
 */
@RestController
@CrossOrigin
@Api(tags = "日志")
@RequestMapping(value = "log")
public class LogController {

    @Autowired
    private OpLogService logService;

    @GetMapping(value = "select/all")
    @ApiOperation(value = "查询所有日志")
    public Object selectAll(){
        return Result.success(logService.selectAll());
    }

    @GetMapping(value = "select/userId")
    @ApiOperation(value = "查询特定用户日志")
    public Object selectById(Integer id){
        return Result.success(logService.selectById(id));
    }

    @GetMapping(value = "select/ip")
    @ApiOperation(value = "查询特定ip日志")
    public Object selectByIp(String ip){
        return Result.success(logService.selectByIp(ip));
    }

    @GetMapping(value = "select/time")
    @ApiOperation(value = "查询时间区域日志")
    public Object selectByTime(String S,String E){
        return Result.success(logService.selectByTime(S,E));
    }

    @PostMapping(value = "insert")
    @ApiOperation(value = "插入数据")
    public Object insert(@RequestBody LogList logList){
        try {
            int count=0;
            for (OpLog log :
                    logList.getLogList()) {
                logService.insert(log);
                count++;
            }
            return Result.success("成功插入"+count+"条数据");
        }catch (Exception e){
            e.printStackTrace();
            return Result.error(4001,"插入失败","");
        }

    }

    @PostMapping(value = "delete")
    @ApiOperation(value = "删除数据")
    public Object insert(@RequestBody IdList idList){
        System.out.println(idList);
        int count=0;
        try {
            for (Integer id:
                    idList.getIdList()) {
                logService.delete(id);
                count++;
            }
            return Result.success("成功删除"+count+"条数据");
        }catch (Exception e){
            e.printStackTrace();
            return Result.error(4001,"删除失败","");
        }
    }

    @PostMapping(value = "update")
    @ApiOperation(value = "更新数据")
    public Object update(@RequestBody LogList logList){
        try {
            int count=0;
            for (OpLog log :
                    logList.getLogList()) {
                logService.update(log);
                count++;
            }
            return Result.success("成功更新"+count+"条数据");
        }catch (Exception e){
            e.printStackTrace();
            return Result.error(4001,"更新失败","");
        }
    }
}
